local osFengji = fk.CreateSkill {
  name = "md__fengji"
}

Fk:loadTranslationTable{
  ["md__fengji"] = "蜂集",
  [":md__fengji"] = "出牌阶段开始时，若你没有“示”，你可将一张非装备牌置于武将牌上，称为“示”并<a href='os__shifa_href'>施法</a>X=1~3回合：" ..
  "从牌堆中获得X张与“示”同名的牌，然后将“示”置入弃牌堆。",

  ["os__revelation"] = "示",
  ["@os__fengji"] = "蜂集",
  ["#md__fengji-ask"] = "蜂集：你可将一张非装备牌置于武将牌上，称为“示”并施法",
  ["#md__fengji-conjure"] = "蜂集：施法，第X个回合结束前，从牌堆中获得X张与“示”同名的牌，然后将“示”置入弃牌堆",

  ["$md__fengji1"] = "蜂趋蚁附，皆为道来。",
  ["$md__fengji2"] = "蜂攒蚁集，皆为道往！",
}

-- 主技能
osFengji:addEffect(fk.EventPhaseStart, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player)
    return
      target == player and
      player:hasSkill(osFengji.name) and
      player.phase == Player.Play and
      not player:isNude() and
      #player:getPile("os__revelation") == 0 and
      player:getMark("@os__fengji") == 0
  end,
  on_cost = function(self, event, target, player)
    local cids = player.room:askToCards(
      player,
      {
        min_num = 1,
        max_num = 1,
        include_equip = false,
        pattern = ".|.|.|.|.|^equip",
        skill_name = osFengji.name,
        prompt = "#md__fengji-ask",
      }
    )
    if #cids > 0 then
      event:setCostData(self, cids[1])
      return true
    end
  end,
  on_use = function(self, event, target, player)
    ---@type string
    local skillName = osFengji.name
    local cost_data = event:getCostData(self)
    player:addToPile("os__revelation", cost_data, true, skillName, player)
    local num = player.room:askToChoice(
      player,
      {
        choices = { "1", "2", "3" },
        skill_name = skillName,
        prompt = "#md__fengji-conjure",
      }
    )
    player.room:setPlayerMark(player, "@os__fengji", num .. "-" .. num)
  end,
})

-- 子技能
osFengji:addEffect(fk.TurnEnd, {
  is_delay_effect = true,
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player)
    return player:getMark("@os__fengji") ~= 0 and string.sub(player:getMark("@os__fengji"), -1) == "0"
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player)
    ---@type string
    local skillName = osFengji.name
    local room = player.room
    local nums = string.split(player:getMark("@os__fengji"), "-")
    local num = tonumber(nums[1])
    if #player:getPile("os__revelation") > 0 then
      room:notifySkillInvoked(player, "md__fengji")
      player:broadcastSkillInvoke("md__fengji")
      room:obtainCard(
        player,
        room:getCardsFromPileByRule(Fk:getCardById(player:getPile("os__revelation")[1]).trueName, num),
        false,
        fk.ReasonPrey,
        player,
        skillName
      )
      room:moveCardTo(
        Fk:getCardById(player:getPile("os__revelation")[1]),
        Card.DiscardPile,
        nil,
        fk.ReasonPutIntoDiscardPile,
        skillName,
        "os__revelation"
      )
    end
    room:setPlayerMark(player, "@os__fengji", 0)
  end,

  can_refresh = function(self, event, target, player)
    return player:getMark("@os__fengji") ~= 0
  end,
  on_refresh = function(self, event, target, player)
    local room = player.room
    local nums = string.split(player:getMark("@os__fengji"), "-")
    local num = tonumber(nums[1])
    local num2 = tonumber(nums[2]) - 1
    room:setPlayerMark(player, "@os__fengji", num .. "-" .. num2)
  end,
})

return osFengji
